package com.qianfeng.shoon.pet.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.qianfeng.shoon.pet.R;
import com.qianfeng.shoon.pet.adapter.MudidiAdapter;
import com.qianfeng.shoon.pet.bean.MudidiEntity;
import com.qianfeng.shoon.pet.ui.ImageIntentActivity;
import com.qianfeng.shoon.pet.utils.AppConfig;

import org.androidannotations.annotations.EFragment;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;
//hello
/**
 * A simple {@link Fragment} subclass.
 */

public class MudidiFragment extends Fragment  implements MudidiAdapter.CallBack {

    private PullToRefreshListView ptr;
    private ListView lv;
    public MudidiFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_mudidi, container, false);
                initView(view);

        initData();
        adapter=new MudidiAdapter(elementsEntityList,getActivity());



        adapter.setCallBack(this);
        return view;
    }


    private AsyncHttpClient client=new AsyncHttpClient();
    private List<MudidiEntity.ElementsEntity> elementsEntityList=new ArrayList<>();
    private MudidiAdapter adapter;
  //  private List<MudidiEntity.ElementsEntity.DataEntity> dataEntityList =new ArrayList<>();
    private void initData() {
        client.get(getActivity(), AppConfig.MUDIDI_URL, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                MudidiEntity entity= JSONObject.parseObject(responseString,MudidiEntity.class);
                elementsEntityList.addAll(entity.getElements());
                lv.setAdapter(adapter);
            }
        });
    }

    private void initView(View view) {
       lv= (ListView) view.findViewById(R.id.lv);

    }


    @Override
    public void call(String id, int type) {
        Intent intent=new Intent(getActivity(), ImageIntentActivity.class);
        intent.putExtra("id",id);
        intent.putExtra("type",type);
        startActivity(intent);
    }
}
